<?php

namespace addons\booking\controller;

use fast\Tree;
use addons\booking\model\GoodsCategory;
use addons\booking\model\Goods as GoodsModel;
use addons\booking\model\Collect;

/**
 * 商品
 */
class Goods extends Base
{
    protected $noNeedLogin = ['category', 'index', 'detail'];

    /**
     * 商品分类接口
     */
    public function category()
    {
        $category_mode = $this->request->param('category_mode');
        if ($category_mode == 1) { //一级分类
            $categoryList = GoodsCategory::where('pid', 0)->field('id,name,image')->order('weigh desc,id desc')->select();
            $categoryList = collection($categoryList)->toArray();
            $this->success('获取成功', $categoryList);
        } else {
            $categoryList = GoodsCategory::field('id,pid,name,image')->order('weigh desc,id desc')->select();
            $categoryList = collection($categoryList)->toArray();
            $tree = Tree::instance();
            $tree->init($categoryList, 'pid');
            $categoryList = $tree->getTreeArray(0);
            $this->success('获取成功', $categoryList);
        }
    }

    /**
     * 商品列表
     */
    public function index()
    {
        $this->success('', GoodsModel::tableList($this->request->param()));
    }

    /**
     * 商品详情
     */
    public function detail()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误');
        }
        $detail = GoodsModel::detail($id);
        if (!$detail) {
            $this->error('未找到商品数据');
        }
        $detail->is_collect = Collect::isCollect(1, $id);
        $this->success('获取成功', $detail);
    }

}
